package com.tj.jinzao.presenter;


import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Case;
import com.tj.jinzao.model.ResultResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * 病历
 * Created by wjy 2018/12/10.
 */
public class CasePresenter extends BasePresenter<IUploadView<List<Case>>> {

    public CasePresenter(IUploadView<List<Case>> view) {
        super(view);
    }

    public void getCaseList(String uid) {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.getCaseList(Long.parseLong(uid))
                , new SubscriberCallBack<List<Case>>(mView.getContext()) {
                    @Override
                    protected void onSuccess(List<Case> list) {
                        if (mView != null) mView.onRequestSuccess(list);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }
                });
    }

    public void getCaseById(long id) {
        addSubscription(mApiService.getCase(id)
                , new SubscriberCallBack<Case>(mView.getContext()) {
                    @Override
                    protected void onSuccess(Case bean) {
                        ArrayList<Case> list = new ArrayList();
                        list.add(bean);
                        if (mView != null) mView.onRequestSuccess(list);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }
                });
    }

    public void addCase(String name, String path) {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.addCase(uId, name, path)
                , new SubscriberCallBack<Case>(mView.getContext()) {
                    @Override
                    protected void onSuccess(Case list) {
                        if (mView != null) mView.onRequestSuccess(null);
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }
                });
    }

    @Override
    protected void onUploadSuccess(String localPath, String response) {
        if (mView != null) mView.onUploadSuccess(localPath, response);
    }
}